package com.healthcloud.service.impl;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.HashMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.healthcloud.service.HealthReportService;
import com.healthcloud.vo.PhysicalExamVo;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;

/**
 * 
 * @author ZhengL 2015-07-30
 */
@Service
public class HealthReportServiceImpl implements HealthReportService {
    
	@Override
	public void createPDF(PhysicalExamVo v, String filePath,HashMap<String,String[]> tableMap) throws Exception {
		try {
			Document doc = new Document(PageSize.A4);
			PdfWriter.getInstance(doc, new FileOutputStream(filePath));
			doc.open();
			// 设置中文字体
			BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
			Font FontChinese = new Font(bfChinese, 14, Font.BOLD);
			Font FontChinese2 = new Font(bfChinese, 10, Font.NORMAL);
			Font FontChinese3 = new Font(bfChinese, 10, Font.NORMAL,BaseColor.RED);
			PdfPTable table1 = createReportHead(v, FontChinese, FontChinese2);
			doc.add(table1);
			
			//表头分割线
			Paragraph p1 = new Paragraph(new Chunk(new LineSeparator(1, 100,null, Element.ALIGN_BOTTOM, 5)));
			Paragraph p2 = new Paragraph(new Chunk(new LineSeparator(1, 100,null, Element.ALIGN_TOP, 15)));
			doc.add(p1);
			doc.add(p2);
			
			//病人信息
			PdfPTable table2 = createReportUserInfo(v,FontChinese2);
			doc.add(table2);
			
			String[] tableList = tableMap.get("tableList");
			for(int i=0;i<tableList.length;i++){
				String tableStr = "table"+(i+1);
				String table_fcode_Str = "table"+(i+1)+"_fcode";
				String[] table = tableMap.get(tableStr);
				String[] table_fcode = tableMap.get(table_fcode_Str);
				
				if(i==6){//分页
					doc.add(table1);
					doc.add(p1);
					doc.add(p2);
					doc.add(table2);
				}
				
				//分割线
				Paragraph p = new Paragraph(new Chunk(new LineSeparator(0, 100,BaseColor.LIGHT_GRAY, Element.ALIGN_BOTTOM, 5)));
				doc.add(p);
				
				PdfPTable pdfTable = new PdfPTable(2);
				pdfTable.setTotalWidth(160);
				pdfTable.setWidths(new float[] { 80f, 80f });

				PdfPCell cell = new PdfPCell();
				cell.setPhrase(new Paragraph(tableList[i] +"("+v.getExamDateStr()+")",FontChinese2));
				cell.setBorderColor(BaseColor.WHITE);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(2);
				cell.setMinimumHeight(20f);
				pdfTable.addCell(cell);
				
				for(int t=0;t<table.length;t++){
					cell = new PdfPCell();
					cell.setBorderColor(BaseColor.WHITE);
					cell.setHorizontalAlignment(Element.ALIGN_LEFT);
					String value = v.getResultMap().get(table_fcode[t]);
					String result = v.getResultMap().get(table_fcode[t]+"_result");
					if(StringUtils.isBlank(value)){
						value="";
					}
					if(table[t]=="结论"||table[t].equals("结论")){
						cell.setPhrase(new Paragraph("结论：" + value, FontChinese2));
						cell.setColspan(2);
					}else{
						if(t==table.length-2&&(table.length)%2==0){
							if(StringUtils.isNotBlank(result)){
								if(result.equals("0")||result=="0"){
									cell.setPhrase(new Paragraph(table[t]+": "+" "+value,FontChinese2));
								}else{
									cell.setPhrase(new Paragraph(table[t]+": "+" "+value,FontChinese3));//异常值标红
								}
							}else{
								cell.setPhrase(new Paragraph(table[t]+": "+" "+value,FontChinese2));
							}
							cell.setColspan(2);
						}else{
							if(StringUtils.isNotBlank(result)){
								if(result.equals("0")||result=="0"){
									cell.setPhrase(new Paragraph(table[t]+": "+" "+value,FontChinese2));
								}else{
									cell.setPhrase(new Paragraph(table[t]+": "+" "+value,FontChinese3));//异常值标红
								}
							}else{
								cell.setPhrase(new Paragraph(table[t]+": "+" "+value,FontChinese2));
							}
						}
					}
					cell.setMinimumHeight(20f);
					pdfTable.addCell(cell);
				}
				if(i==5){
					cell = new PdfPCell();
					pdfTable.setExtendLastRow(true); 
					cell.setBorderColor(BaseColor.WHITE);
					pdfTable.addCell(cell);
				}
				doc.add(pdfTable);
			}
			
			Paragraph p = new Paragraph(new Chunk(new LineSeparator(0, 100,BaseColor.LIGHT_GRAY, Element.ALIGN_BOTTOM, 5)));
			doc.add(p);
			PdfPTable table10 = new PdfPTable(1);
			table10.setTotalWidth(100);
			table10.setWidths(new float[] { 100f });
			PdfPCell cell = new PdfPCell();
			String res1 = v.getDietaryGuidanceResult();
			if(StringUtils.isBlank(res1)){
				res1 = "";
			}
			cell.setPhrase(new Paragraph("膳食指导：\n" +" "+res1+"\n", FontChinese2));
			cell.setBorderColor(BaseColor.WHITE);
			cell.setHorizontalAlignment(Element.ALIGN_LEFT);
			table10.addCell(cell);
			cell = new PdfPCell();
			String res2 = v.getSportGuidanceResult();
			if(StringUtils.isBlank(res2)){
				res2 = "";
			}
			cell.setPhrase(new Paragraph("运动建议：\n" + " "+res2+"\n", FontChinese2));
			cell.setBorderColor(BaseColor.WHITE);
			cell.setHorizontalAlignment(Element.ALIGN_LEFT);
			table10.addCell(cell);
			cell = new PdfPCell();
			String res3 = v.getDoctorSummary();
			if(StringUtils.isBlank(res3)){
				res3 = "";
			}
			cell.setPhrase(new Paragraph("医生总结：\n" + " "+res3+"\n", FontChinese2));
			cell.setBorderColor(BaseColor.WHITE);
			cell.setHorizontalAlignment(Element.ALIGN_LEFT);
			table10.addCell(cell);
			cell = new PdfPCell();
			cell.setPhrase(new Paragraph("",FontChinese2));
			cell.setBorderColor(BaseColor.WHITE);
			cell.setHorizontalAlignment(Element.ALIGN_LEFT);
			table10.addCell(cell);
			cell = new PdfPCell();
			cell.setBorderColor(BaseColor.WHITE);
			table10.setExtendLastRow(true);  
			table10.addCell(cell);  
			doc.add(table10);
			doc.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	
	// 创建pdf头部,体检报告标题
	private PdfPTable createReportHead(PhysicalExamVo v, Font f1, Font f2)
			throws Exception {
		PdfPTable table1 = new PdfPTable(3);
		// table1.setWidthPercentage(100);
		table1.setTotalWidth(150);
		table1.setWidths(new float[] { 50f, 50f, 50f });
		PdfPCell cell1 = new PdfPCell();
		cell1.setColspan(3);
		String hospitalName = v.getHospitalName();
		if(StringUtils.isBlank(hospitalName)){
			hospitalName="宁波美康中医院";
		}
		Paragraph para = new Paragraph(hospitalName, f1);
		cell1.setPhrase(para);
		cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
		cell1.setBorderColor(BaseColor.WHITE);
		cell1.setMinimumHeight(30f);
		table1.addCell(cell1);
		PdfPCell cell2 = new PdfPCell();
		cell2.setPhrase(new Paragraph("体检编号：" + v.getMkExamCode(), f2));
		cell2.setBorderColor(BaseColor.WHITE);
		cell2.setNoWrap(true);
		cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
		table1.addCell(cell2);
		PdfPCell cell3 = new PdfPCell();
		cell3.setPhrase(new Paragraph("自主体检汇总报告单", f2));
		cell3.setBorderColor(BaseColor.WHITE);
		cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
		table1.addCell(cell3);
		PdfPCell cell4 = new PdfPCell();
		cell4.setPhrase(new Paragraph("体检时间：" + v.getExamDateStr(), f2));
		cell4.setBorderColor(BaseColor.WHITE);
		cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
		table1.addCell(cell4);
		return table1;
	}

	// 创建pdf病人信息部分
	private PdfPTable createReportUserInfo(PhysicalExamVo v, Font f1)
			throws Exception {
		PdfPTable table2 = new PdfPTable(5);
		table2.setTotalWidth(180);
		table2.setWidths(new float[] { 80f, 50f, 50f, 50f, 50f });
		PdfPCell cell = new PdfPCell();
		cell.setPhrase(new Paragraph("姓名：" + v.getPatientName(), f1));
		cell.setBorderColor(BaseColor.WHITE);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setMinimumHeight(20f);
		table2.addCell(cell);
		cell = new PdfPCell();
		cell.setPhrase(new Paragraph("性别：" + v.getSex(), f1));
		cell.setBorderColor(BaseColor.WHITE);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table2.addCell(cell);
		cell = new PdfPCell();
		cell.setPhrase(new Paragraph("年龄：" + v.getAge(), f1));
		cell.setBorderColor(BaseColor.WHITE);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table2.addCell(cell);
		cell = new PdfPCell();
		cell.setPhrase(new Paragraph("卡号：" + v.getMedicalCardNum(), f1));
		cell.setBorderColor(BaseColor.WHITE);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table2.addCell(cell);
		cell = new PdfPCell();
		cell.setBorderColor(BaseColor.WHITE);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setPhrase(new Paragraph("", f1));
		cell.setRowspan(2);
		table2.addCell(cell);
		cell = new PdfPCell();
		cell.setPhrase(new Paragraph("电话：" + v.getPhone(), f1));
		cell.setBorderColor(BaseColor.WHITE);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table2.addCell(cell);
		cell = new PdfPCell();
		cell.setPhrase(new Paragraph("地址：" + v.getHomeAddress(), f1));
		cell.setBorderColor(BaseColor.WHITE);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setColspan(3);
		table2.addCell(cell);
		return table2;
	}

	
}
